package fy.mdm.dao;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.soa.eis.adapter.framework.exception.EisException;
import com.soa.eis.adapter.framework.message.IMsgObject;
import com.soa.eis.adapter.framework.message.impl.GroupRecord;
import com.soa.eis.adapter.framework.message.impl.MsgObject;
import com.soa.eis.adapter.framework.requester.IServiceRequester;
import com.soa.eis.adapter.framework.requester.impl.BaseServiceRequester;

public class MeasurementUnitMapping {
	String contr = null;

	public String getMeasurementUnit(String distinguish, String keyValue) throws EisException {

		if (distinguish.equals("1")) {
			contr = "BASEKEY";
		} else {
			contr = "BASEVALUE";
		}
		/*
		 * <?xml version="1.0" encoding="UTF-8"?>
		 * 
		 * <Service>   <Route>     <ServiceResponse/>
		 *     <SourceSysID>02007</SourceSysID>
		 *     <ServiceID>06001000000010</ServiceID>
		 *     <SerialNO>2017082202007000001</SerialNO>
		 *     <ServiceTime>20170822095808</ServiceTime>   </Route>   <Data>
		 *     <Control/>     <Request>       <BOID>MDM_PROIDUNCT_OEM_F</BOID>
		 *       <PageNum>1</PageNum>       <SearchRecordNum></SearchRecordNum>
		 *       <OrderField></OrderField>       <OrderType>Asc</OrderType>
		 *       <CaseSensitive>true</CaseSensitive>
		 *       <Distinct>true</Distinct>       <SearchFields></SearchFields>
		 *       <Condition p_type="G" loop_num="1" logicConditionType="and">
		 *         <ROW p_type="G" loop_num="1">
		 *           <ControlType>equals</ControlType>
		 *           <Key>EXTEND_H</Key>           <Value>A</Value>
		 *         </ROW>         <ROW p_type="G" loop_num="2">
		 *           <ControlType>equals</ControlType>
		 *           <Key>PARTNO</Key>           <Value>52A32A033</Value>
		 *         </ROW>       </Condition>     </Request>     <Response/>
		 *   </Data> </Service>
		 */

		/*
		 * IMsgObject respMo = null; IMsgObject reqMo = new
		 * MsgObject(IMsgObject.MOType.initSR); reqMo.setSourceSysID("04002");
		 * reqMo.setSerialNO(GenSerialNoUtil.createSerialNO());
		 * reqMo.setServiceID(HandlerAdapter.MDM_SEARCH_COMMON);
		 * reqMo.setServiceDateTime(ReqOrResMoUtil.genReqServiceTime());
		 * List<GroupRecord> grs2 = new ArrayList<GroupRecord>(); // 操作类型，固定值5
		 * reqMo.setReqValue("BOID", "V_MDM_HALF_PINT_OTHER_T_SQ"); //
		 * reqMo.setReqValue("BOID", "soa_mdm_supplier_basic_f");
		 * reqMo.setReqValue("PageNum", "1");
		 * reqMo.setReqValue("SearchRecordNum", "100");
		 * reqMo.setReqValue("OrderField", ""); reqMo.setReqValue("OrderType",
		 * "Asc"); reqMo.setReqValue("CaseSensitive", "true");
		 * reqMo.setReqValue("Distinct", "true");
		 * reqMo.setReqValue("SearchFields",
		 * "HP_NUMBER,PRODUCT_TYPE,PRDUCT_ID,EXTEND_C"); // ======gr2=======//
		 * GroupRecord gr1 = new GroupRecord(); gr1.setName("Condition");
		 * gr1.setAttribute("logicConditionType", "and"); List<GroupRecord>
		 * gRecords = new ArrayList<GroupRecord>(); GroupRecord grRow = new
		 * GroupRecord(); grRow.setName("ROW");
		 * grRow.setFieldValue("ControlType", "EQUALS");
		 * grRow.setFieldValue("Key", "HP_NUMBER"); grRow.setFieldValue("Value",
		 * "%b76%"); gRecords.add(grRow);
		 * 
		 * List<GroupRecord> gRecords1 = new ArrayList<GroupRecord>();
		 * GroupRecord gr2 = new GroupRecord(); gr2.setName("Condition");
		 * gr2.setAttribute("logicConditionType", "and"); GroupRecord grRow1 =
		 * new GroupRecord(); grRow1.setName("ROW");
		 * grRow1.setFieldValue("ControlType", "EQUALS");
		 * grRow1.setFieldValue("Key", "PRDUCT_ID");
		 * grRow1.setFieldValue("Value", "%qqqqqqq%"); gRecords1.add(grRow);
		 * 
		 * 
		 * gr2.setSubGroups("ROW", gRecords1); gr1.setSubGroups("ROW",
		 * gRecords); grs2.add(gr1); grs2.add(gr2);
		 * reqMo.setReqGroupRecord(grs2);
		 */
		IServiceRequester Requester = BaseServiceRequester.getInstance();
		List<GroupRecord> grs1 = new ArrayList<GroupRecord>();
		List<GroupRecord> grs2 = new ArrayList<GroupRecord>();
		// 定义请求报文对象
		IMsgObject reqMo = null;
		IMsgObject respMo = null;
		String basevalue = null;

		try {
			reqMo = new MsgObject(IMsgObject.MOType.initSR);
			String SourceSysID = "06004"; // 请求方(发布方)
			String ServiceID = "06001000000010";// 响应方(发布方)
			// 获取系统时间
			Date date = new Date();
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
			String format = simpleDateFormat.format(date);
			// 对请求报文对象进行设置
			reqMo.setSourceSysID(SourceSysID); // 设置报文 请求方系统号
			reqMo.setSerialNO(format.substring(0, 8) + SourceSysID
					+ String.format("%06d", ++MaterialRequestNoTimerTask.requestNo));// 设置报文流水号,第三段表示今天发送的第几的一次报文
			reqMo.setServiceID(ServiceID); // 设置报文 提供方服务号
			reqMo.setServiceDateTime(format); // 设置报文 日期时间
			/*
			 *   <PageNum>1</PageNum>       <SearchRecordNum></SearchRecordNum>
			 *       <OrderField></OrderField>       <OrderType>Asc</OrderType>
			 *       <CaseSensitive>true</CaseSensitive>
			 *       <Distinct>true</Distinct>
			 *       <SearchFields></SearchFields>
			 */
			reqMo.setReqValue("BOID", "CODE_BASE_DATA_A");
			reqMo.setReqValue("PageNum", "1");
			reqMo.setReqValue("SearchRecordNum", "10");
			reqMo.setReqValue("OrderField", contr);
			reqMo.setReqValue("OrderType", "asc");
			reqMo.setReqValue("CaseSensitive", "true");
			reqMo.setReqValue("Distinct", "true");
			reqMo.setReqValue("SearchFields", "BASEKEY,BASEVALUE");

			/*
			 * GroupRecord gr1 = new GroupRecord(); gr1.setName("ROW");
			 * gr1.setFieldValue("ControlType","EQUALS");
			 * gr1.setFieldValue("Key","BASEKEY"); gr1.setFieldValue("Value",
			 * keyValue); grs1.add(gr1);
			 */

			GroupRecord gr1 = new GroupRecord();
			gr1.setName("ROW");
			gr1.setFieldValue("ControlType", "EQUALS");
			gr1.setFieldValue("Key", contr);
			gr1.setFieldValue("Value", keyValue);
//			gr1.setFieldValue("FILTERCODE","A000001");

			grs1.add(gr1);			
			
			GroupRecord gr2 = new GroupRecord();
			gr2.setName("ROW");
			gr2.setFieldValue("ControlType", "EQUALS");
			gr2.setFieldValue("Key", "FILTERCODE");
			gr2.setFieldValue("Value", "A000001");
			grs1.add(gr2);	
			/*
			 * GroupRecord gr2 = new GroupRecord(); gr2.setName("ROW");
			 * gr2.setFieldValue("ControlType","EQUALS");
			 * gr2.setFieldValue("Key","MATERIEL_CODE");
			 * gr2.setFieldValue("Value", "ABB160019700350C"); grs1.add(gr2);
			 */
			GroupRecord gr3 = new GroupRecord();
			gr3.setName("Condition");
			gr3.setSubGroups("value", grs1);
			gr3.setType("G");
			gr3.setAttribute("logicConditionType", "and");
			grs2.add(gr3);

			// grs2.addAll(grs1);

			reqMo.setReqGroupRecord(grs2);
	
			
			respMo = Requester.execute(reqMo);
			List<GroupRecord> record2 = respMo.getResGroupRecord("ROW");
			for (GroupRecord groupRecord : record2) {
				
				if (distinguish.equals("1")) {
					basevalue = groupRecord.getFieldValue("BASEVALUE");
				} else {
					basevalue = groupRecord.getFieldValue("BASEKEY");
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return basevalue;
	}

}
